using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
using MvcExtExample.Models;

namespace MvcExtExample.ReportViews
{
    public partial class OilDQBReport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["LogedUser"] == null)
                Response.Redirect("http://localhost:8132/home/index");
            if (!IsPostBack)
            {
                //获取所有工作区的名称
                this.WorkZoneDropDownList.DataSource = GetWorkZoneData();
                this.WorkZoneDropDownList.DataBind();
                //获取“第一作业区”下的生产大队列表
                this.SCDDDropDownList.DataSource = GetSCDDData("第一作业区");
                this.SCDDDropDownList.DataBind();
                //获取“第一生产大队”下的生产小队列表
                this.SCXDDropDownList.DataSource = GetSCDDData("第一采油大队");
                this.SCXDDropDownList.DataBind();
                //获取“第一采油小队”下的电潜泵井列表
                this.DQBJDropDownList.DataSource = GetDQBJData("第一采油小队");
                this.DQBJDropDownList.DataBind();
                this.productdate_box.Value = DateTime.Now.ToString("yyyy-MM-dd");
                DataTable dt = GetData();
                ReportDataSource rds = new ReportDataSource("DQBJOilDataSet", dt);
                this.ReportViewer1.LocalReport.ReportPath = Server.MapPath("..\\Reports\\OilDQBReport.rdlc");
                ReportViewer1.KeepSessionAlive = true;//解决ASP.NET session has expired or could not be found问题
                ReportViewer1.LocalReport.DataSources.Clear();
                ReportViewer1.LocalReport.DataSources.Add(rds);
                ReportViewer1.LocalReport.Refresh();
            }
        }

        //从数据库读取报表所需数据
        private DataTable GetData()
        {
            List<string> Well_ID = new List<string>(), Well_Section = new List<string>(), time = new List<string>(), DailyFluid = new List<string>(), DailyOil = new List<string>(), DailyGas = new List<string>(), Gas_Oil_Ratio = new List<string>(), Moisture_Content = new List<string>(), Sand_Content = new List<string>(), Wellhead_Temperature = new List<string>(), Add_Water = new List<string>(), Comment = new List<string>(), Nozzle_Diameter = new List<string>(), Pump_Type = new List<string>(), Output_Volume = new List<string>();
            string productdate = this.productdate_box.Value;
            DateTime dt_pd = Convert.ToDateTime(productdate);
            string workZone = this.WorkZoneDropDownList.SelectedValue;
            string SCDD = this.SCDDDropDownList.SelectedValue;
            string DQBJ = this.DQBJDropDownList.SelectedValue;
            string strSqlStr = "SELECT  * FROM [dbo].[Tb_Oil_Well]";
            if (DQBJDropDownList.SelectedValue == "--全部--")//那么就把所有这个日期的电潜泵井的数据读出来
            {
                strSqlStr = "SELECT  * FROM [dbo].[Tb_Oil_Well] WHERE Date = '" + dt_pd + "' AND SUBSTRING([Well_ID], CHARINDEX('_',[Well_ID])+1, 4) = 'DQBJ' AND SUBSTRING([Well_ID],LEN([Well_ID])-3,1) ='1' AND SUBSTRING([Well_ID],LEN([Well_ID])-2,1) ='1'";
            }
            else
            {
                strSqlStr = "SELECT  * FROM [dbo].[Tb_Oil_Well] WHERE Date = '" + dt_pd + "' AND [Well_ID]='" + DQBJ + "'";
            }
            framework.SqlHelperCS.SqlHelper cs = new framework.SqlHelperCS.SqlHelper();
            DataSet DSet = cs.GetDataSet(strSqlStr);
            if (DSet != null)
            {
                DataTable DTable = DSet.Tables[0];
                for (int i = 0; i < DTable.Rows.Count; i++)
                {
                    DataRowView rowview = DSet.Tables[0].DefaultView[i];
                    Well_ID.Add(rowview["Well_ID"].ToString());
                    Well_Section.Add(rowview["Well_Section"].ToString());
                    time.Add(rowview["time"].ToString());
                    DailyFluid.Add(rowview["DailyFluid"].ToString());
                    DailyOil.Add(rowview["DailyOil"].ToString());
                    DailyGas.Add(rowview["DailyGas"].ToString());
                    Gas_Oil_Ratio.Add(rowview["Gas_Oil_Ratio"].ToString());
                    Moisture_Content.Add(rowview["Moisture_Content"].ToString());
                    Sand_Content.Add(rowview["Sand_Content"].ToString());
                    Wellhead_Temperature.Add(rowview["Wellhead_Temperature"].ToString());
                    Add_Water.Add(rowview["Add_Water"].ToString());
                    Comment.Add(rowview["Comment"].ToString());
                    Nozzle_Diameter.Add(rowview["Nozzle_Diameter"].ToString());
                    Pump_Type.Add(rowview["Pump_Type"].ToString());
                    Output_Volume.Add(rowview["Output_Volume"].ToString());
                }
            }

            DataTable dt = new DataTable("CYJOilDataTable");//这个名字是无关紧要的，xsd的DataTable也可以随意命名
            dt.Columns.Add(new DataColumn("Well_ID", typeof(string)));
            dt.Columns.Add(new DataColumn("Well_Section", typeof(string)));
            dt.Columns.Add(new DataColumn("time", typeof(string)));
            dt.Columns.Add(new DataColumn("DailyFluid", typeof(string)));
            dt.Columns.Add(new DataColumn("DailyOil", typeof(string)));
            dt.Columns.Add(new DataColumn("DailyGas", typeof(string)));
            dt.Columns.Add(new DataColumn("Gas_Oil_Ratio", typeof(string)));
            dt.Columns.Add(new DataColumn("Moisture_Content", typeof(string)));
            dt.Columns.Add(new DataColumn("Sand_Content", typeof(string)));
            dt.Columns.Add(new DataColumn("Wellhead_Temperature", typeof(string)));
            dt.Columns.Add(new DataColumn("Add_Water", typeof(string)));
            dt.Columns.Add(new DataColumn("Comment", typeof(string)));
            dt.Columns.Add(new DataColumn("Nozzle_Diameter", typeof(string)));
            dt.Columns.Add(new DataColumn("Pump_Type", typeof(string)));
            dt.Columns.Add(new DataColumn("Output_Volume", typeof(string)));
            for (int i = 0; Well_ID.Count > 0 && i < Well_ID.Count; i++)
            {
                DataRow row = dt.NewRow();
                row["Well_ID"] = Well_ID[i];
                row["Well_Section"] = Well_Section[i];
                row["time"] = time[i];
                row["DailyFluid"] = DailyFluid[i];
                row["DailyOil"] = DailyOil[i];
                row["DailyGas"] = DailyGas[i];
                row["Gas_Oil_Ratio"] = Gas_Oil_Ratio[i];
                row["Moisture_Content"] = Moisture_Content[i];
                row["Sand_Content"] = Sand_Content[i];
                row["Wellhead_Temperature"] = Wellhead_Temperature[i];
                row["Add_Water"] = Add_Water[i];
                row["Comment"] = Comment[i];
                row["Nozzle_Diameter"] = Nozzle_Diameter[i];
                row["Pump_Type"] = Pump_Type[i];
                row["Output_Volume"] = Output_Volume[i];
                dt.Rows.Add(row);
            }
            return dt;
        }

        //根据生产小队名称得到电潜泵井列表
        private List<string> GetDQBJData(string strSCXD)
        {
            List<string> DQBJList = new List<string>();
            string strSqlStr = "SELECT [Well_ID] FROM [dbo].[Tb_CommonWell]  WHERE [Dep_ID] IN (SELECT [Dep_ID] FROM [dbo].[Tb_Department] WHERE [Dep_Name] = '" + strSCXD + "') AND SUBSTRING([Well_ID],0,3)='DQ'";
            framework.SqlHelperCS.SqlHelper cs = new framework.SqlHelperCS.SqlHelper();
            DataSet DSet = cs.GetDataSet(strSqlStr);
            if (DSet != null)
            {
                DataTable DTable = DSet.Tables[0];
                for (int i = 0; i < DTable.Rows.Count; i++)
                {
                    DataRowView rowview = DSet.Tables[0].DefaultView[i];
                    DQBJList.Add(rowview["Well_ID"].ToString());
                }
            }
            return DQBJList;
        }

        //获取所有工作区的名称
        private List<string> GetWorkZoneData()
        {
            List<string> WorkZone = new List<string>();
            string strSqlStr = "SELECT [Dep_Name] FROM [dbo].[Tb_Department] WHERE [ParentDep_ID]='ALL'";
            framework.SqlHelperCS.SqlHelper cs = new framework.SqlHelperCS.SqlHelper();
            DataSet DSet = cs.GetDataSet(strSqlStr);
            if (DSet != null)
            {
                DataTable DTable = DSet.Tables[0];
                for (int i = 0; i < DTable.Rows.Count; i++)
                {
                    DataRowView rowview = DSet.Tables[0].DefaultView[i];
                    WorkZone.Add(rowview["Dep_Name"].ToString());
                }
            }
            return WorkZone;
        }

        protected void WorkZoneDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.SCDDDropDownList.DataSource = GetSCDDData(WorkZoneDropDownList.SelectedItem.Text);
            this.SCDDDropDownList.DataBind();
        }

        //根据工作区名称得到生产大队列表
        private List<string> GetSCDDData(string strSCDD)
        {
            List<string> SCDDList = new List<string>();
            string strSqlStr = "SELECT [Dep_Name] FROM [dbo].[Tb_Department] WHERE [ParentDep_ID] IN (SELECT [Dep_ID] FROM [dbo].[Tb_Department] WHERE [Dep_Name] = '" + strSCDD + "') AND SUBSTRING([dbo].[Tb_Department].[Dep_ID],0,3)='CY'";
            framework.SqlHelperCS.SqlHelper cs = new framework.SqlHelperCS.SqlHelper();
            DataSet DSet = cs.GetDataSet(strSqlStr);
            if (DSet != null)
            {
                DataTable DTable = DSet.Tables[0];
                for (int i = 0; i < DTable.Rows.Count; i++)
                {
                    DataRowView rowview = DSet.Tables[0].DefaultView[i];
                    SCDDList.Add(rowview["Dep_Name"].ToString());
                }
            }
            return SCDDList;
        }

        protected void queryBtn_Click(object sender, EventArgs e)
        {
            DataTable dt = GetData();
            ReportDataSource rds = new ReportDataSource("DQBJOilDataSet", dt);
            this.ReportViewer1.LocalReport.ReportPath = Server.MapPath("..\\Reports\\OilDQBReport.rdlc");
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(rds);
            ReportViewer1.LocalReport.Refresh();
        }

        protected void SCDDDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.DQBJDropDownList.DataSource = GetDQBJData(SCDDDropDownList.SelectedItem.Text);
            this.DQBJDropDownList.DataBind();
        }

        //“生产小队”选项变换时，“抽油机井井号”的选项也要改变
        protected void SCXDDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            DQBJDropDownList.DataSource = GetDQBJData(SCXDDropDownList.SelectedItem.Text);
            DQBJDropDownList.DataBind();
        }

        //选择不同的井型，加载不同的报表
        protected void JXDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (JXDropDownList.SelectedItem.Text)
            {
                case "抽油机井":
                    Response.Redirect("OilCYJReportView.aspx");
                    break;
                case "电潜泵井":
                    break;
                case "螺杆泵井":
                    Response.Redirect("OilLGBReportView.aspx");
                    break;
                case "水力喷射井":
                    Response.Redirect("OilSLPSJReportView.aspx");
                    break;
                default:
                    Response.Redirect("OilZPJReport.aspx");
                    break;
            }
        }

    }
}